package com.yycx.module.bus.provider.handler.settle;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 已结算列表
 *
 * @author zyf
 */
@Component("yesSettlePageListHandler")
public class YesSettlePageListHandler implements PageInterceptor<BusOrderDetails>, ExportInterceptor {

    @Autowired
    private BusPaymentService paymentService;

    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        Long paymentId = params.getLong("paymentId");
        String paymentIds = params.get("paymentIds");
        cq.addSelect(BusOrder.class, "orderNo", "sendDate");
        cq.addSelect(BusOrderDetails.class, "customBond");
        cq.eq(true, "bod.companyId", OpenHelper.getCompanyId());
        if (FlymeUtils.isNotEmpty(paymentId)) {
            cq.and(e -> e.in(true, "bod.jiesuanId", paymentId).or().in(true, "bod.jiesuanId2", paymentId).or().in(true, "bod.jiesuanId3", paymentId));
        }
        if (FlymeUtils.isNotEmpty(paymentIds)) {
            cq.and(e -> e.in(true, "bod.jiesuanId", paymentIds.split(",")).or().in(true, "bod.jiesuanId2", paymentIds.split(",")).or().in(true, "bod.jiesuanId3", paymentIds.split(",")));
        }
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusOrderDetails> cq, List<EntityMap> result, EntityMap extra) {

        if (FlymeUtils.isNotEmpty(result)) {
            for (EntityMap entityMap : result) {
                BigDecimal customBond = entityMap.getBigDecimal("customBond");
                BigDecimal customOverBond = entityMap.getBigDecimal("customOverBond");
                entityMap.put("customBond", customBond.add(customOverBond));
            }
        }
        return result;
    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        if (key.equals("orderNo")) {
            exportField.setWidth(13);
        } else if (key.equals("customName")) {
            exportField.setWidth(20);
        } else if (key.equals("drugsName")) {
            exportField.setWidth(20);
        } else if (key.equals("returnDate")) {
            exportField.setWidth(10);
        } else if (key.equals("sendDate")) {
            exportField.setWidth(10);
        } else {
            exportField.setWidth(9);
        }

    }
}
